#!/bin/sh
#
if [ $# -lt 1 ]; then
    echo "********************************************************************************"
    echo "usage:"
    echo "   sdf2acf design [edif_file dc_script_file sdf_constraint_file sdf_timing_file]"
    echo "example:"
    echo "   sdf2acf test test.edf test_setup.dc test_constraints.sdf test.sdf "
    echo "********************************************************************************"
    exit 0
fi

proj=$1
        if [ -s altsyn.dc ]; then
           mv altsyn.dc $proj"_setup.dc"
        fi
        if [ -s $proj.edif ]; then
           mv $proj.edif $proj.edf
        fi
edif_f=$proj.edf;
dc_wscr_f=$proj"_setup.dc";
sdf_tkrn_f=$proj"_constraints.sdf";
sdf_time_f=$proj.sdf;
 
if [ $# -eq "2" ]; then
    edif_f=$2;
elif [ $# -eq "3" ]; then
    edif_f=$2;
    dc_wscr_f=$3;
elif [ $# -eq 4 ]; then
    edif_f=$2;
    dc_wscr_f=$3;
    sdf_tkrn_f=$4;
elif [ $# -eq 5 ]; then
    edif_f=$2;
    dc_wscr_f=$3;
    sdf_tkrn_f=$4;
    sdf_time_f=$5;
fi


    if  [  ! -s $sdf_tkrn_f ]; then
        echo "Error: Can not open sdf constraint file :  $sdf_tkrn_f"
        exit 0   
    fi    
    if  [  ! -s $sdf_time_f ]; then
        echo "Error: Can not open sdf timing file :  $sdf_time_f"
        exit 0   
    fi    
    if  [  ! -s $dc_wscr_f ]; then
        echo "Error: Can not open dc_shell script file :  $dc_wscr_f"
        exit 0   
    fi    

rm -f alt_tmp.*;

    if [ -w alt_tmp.acf ]; then
        echo "Error: Can not create intermediate file alt_tmp.acf"
        exit 0
    fi

#
# test if environment variable exist
#
alt_home_env=`env | grep ALT_HOME | wc -l`;
if [ $alt_home_env -eq 0 ]; then
ALT_HOME="/usr/maxplus2"
fi

    if [ ! -d $ALT_HOME/synopsys/bin ]; then
	echo "Error: Directory $ALT_HOME/synopsys/bin does not exist"
	exit 0
    fi
################################################################
# read 3 input files separately, concatanate outputs and sort by
# "end_path type" 2 fields.
# output TCO to alt_tmp.acf for outputs without set_output_delay
# with -clock; otherwise .tco, .tpo, .tsu and .fmx are generated
################################################################

awk -f $ALT_HOME/synopsys/bin/rd_sdfk.awk $sdf_tkrn_f > alt_tmp.tkr
    if [ ! -s alt_tmp.tkr ]; then
        echo "Error: No path constraint found in file - $sdf_tkrn_f"
        exit 0
    fi
awk -f $ALT_HOME/synopsys/bin/rd_sdft.awk $sdf_time_f > alt_tmp.csu
awk -f $ALT_HOME/synopsys/bin/rd_wscr.awk $dc_wscr_f > alt_tmp.scr
cat alt_tmp.scr alt_tmp.csu alt_tmp.tkr | sort +0 -1 | \
    awk -f $ALT_HOME/synopsys/bin/rd_dst.awk > alt_tmp.acf
rm -f alt_tmp.tkr alt_tmp.csu alt_tmp.scr;


####################################################################
#                TPD
# sort alt_tmp.tpo by the signal and type
# output delta_tpd , and sort by descending order
# apply minimization maximum constraint algorithm to generate tpd
####################################################################

if [ -s alt_tmp.tpo ] ; then
sort +0 -1 alt_tmp.tpo | \
awk -f $ALT_HOME/synopsys/bin/rd_tpd.awk | \
sort +0 -2 -n -r | \
awk -f $ALT_HOME/synopsys/bin/wt_tpd.awk  >> alt_tmp.acf
rm -f alt_tmp.tpo
fi

####################################################################
#                TCO
# sort alt_tmp.tco file by the signal and type
# adjust tco value for the clock delay
#####################################################################

if [ -s alt_tmp.tco ] ; then
sort +0 -1 alt_tmp.tco | \
awk -f $ALT_HOME/synopsys/bin/wt_tco.awk >> alt_tmp.acf
rm -f alt_tmp.tco
fi


####################################################################
#                TSU
# sort alt_tmp.tsu file by the signal and type
# get minimum tsu for the same input signal
####################################################################

if [ -s alt_tmp.tsu ] ; then
sort +0 -1 alt_tmp.tsu | \
awk -f $ALT_HOME/synopsys/bin/wt_tsu.awk  >> alt_tmp.acf
rm -f alt_tmp.tsu
fi


####################################################################
#                FMAX
# sort alt_tmp.fmx file by the fmax and type
# write fmax for the clock and unique fmax source register with 1
# level of hierarchy;
####################################################################
if [ -s alt_tmp.fmx ] ; then
num_clk=`grep CKCYL alt_tmp.fmx | wc | awk '{print $1}'`
sort +0 -1 -n alt_tmp.fmx | \
awk -f $ALT_HOME/synopsys/bin/wt_fmx.awk param=$num_clk >> alt_tmp.acf
rm -f alt_tmp.fmx
fi
 
####################################################################
# update proj.acf
####################################################################
#

if  [ ! -s $proj.acf ]; then 
        echo $ALT_HOME"/bin/setacf -c -f$proj.acf" > alt_tmp.act; 
fi 

 
if [ -s alt_tmp.spc ]; then 
   if [ -s syn2acf.err ]; then
	rm -f syn2acf.err;
   fi
num_line=`wc alt_tmp.spc  | awk '{print $1+1}'`; 
j=1; 
while  [ $j -lt $num_line ] ; 
do   
        sed -n "$j","$j"p alt_tmp.spc > alt_tmp.sp1; 
        read spec_char < alt_tmp.sp1 
        awk -f $ALT_HOME/synopsys/bin/ch_name.awk char=$spec_char alt_tmp.acf > alt_tmp.chn;
        mv alt_tmp.chn alt_tmp.acf
        j=`expr $j + 1 ` 
 
done 
fi
 
awk '{printf("%s/bin/setacf -f%s.acf -sTIMING_POINT \\\"\\|%s\\\"%s %s %s\n",alt_home,fn,$1,$2,$3,$4)}' alt_home=$ALT_HOME fn=$proj alt_tmp.acf >> alt_tmp.act;

echo $ALT_HOME"/bin/setacf -f$proj.acf -sCOMPILER_INTERFACES_CONFIGURATION EDIF_INPUT_LMF1 altsyn.lmf" >> alt_tmp.act;
echo $ALT_HOME"/bin/setacf -f$proj.acf -sCOMPILER_INTERFACES_CONFIGURATION EDIF_INPUT_VCC VDD" >> alt_tmp.act;
echo $ALT_HOME"/bin/setacf -f$proj.acf -sCOMPILER_INTERFACES_CONFIGURATION EDIF_OUTPUT_VCC VCC" >> alt_tmp.act;

mv alt_tmp.act $proj.act;
rm -f alt_tmp.*
chmod +x $proj.act

echo "Updating $proj.acf ..."

$proj.act > /dev/null
rm -f $proj.act
exit 1
